a = int(input())
b1, b2, b3, b4 = a // 1000, a // 100 % 10, a // 10 % 10, a % 10
b11, b21, b31, b41 = str(b1), str(b2), str(b3), str(b4)
d1, d2, d3, d4, d5 = int(b11 + b21), int(b21 + b31), int(b31 + b41), int(b11 + b21 + b31), int(b21 + b31 + b41)
max_ = 0
if 999 < a < 10000:
    c = d1 - 1
    k = 0
    while c > 1:
       if d1 % c == 0:
            k = 1
       c -= 1
    if k == 0:
        if d1 > max_:
            max_ = d1
    c = d2 - 1
    k = 0
    while c > 1:
       if d2 % c == 0:
            k = 1
       c -= 1
    if k == 0:
        if d2 > max_:
            max_ = d2
    c = d3 - 1
    k = 0
    while c > 1:
       if d3 % c == 0:
            k = 1
       c -= 1
    if k == 0:
        if d3 > max_:
            max_ = d3
    c = d4 - 1
    k = 0
    while c > 1:
       if d4 % c == 0:
            k = 1
       c -= 1
    if k == 0:
        if d4 > max_:
            max_ = d4
    c = d5 - 1
    k = 0
    while c > 1:
       if d5 % c == 0:
            k = 1
       c -= 1
    if k == 0:
        if d5 > max_:
            max_ = d5
    c = b1 - 1
    k = 0
    while c > 1:
       if b1 % c == 0:
            k = 1
       c -= 1
    if k == 0:
        if b1 > max_:
            max_ = b1
    c = b2 - 1
    k = 0
    while c > 1:
       if b2 % c == 0:
            k = 1
       c -= 1
    if k == 0:
        if b2 > max_:
            max_ = b2
    c = b3 - 1
    k = 0
    while c > 1:
       if b3 % c == 0:
            k = 1
       c -= 1
    if k == 0:
        if d1 > max_:
            max_ = b3
    c = b4 - 1
    k = 0
    while c != 1:
       if b4 % c == 0:
            k = 1
       c -= 1
    if k == 0:
        if b4 > max_:
            max_ = b4
    c = a - 1
    k = 0
    while c > 1:
       if a % c == 0:
            k = 1
       c -= 1
    if k == 0:
        if a > max_:
            max_ = a
    print(max_)
else:
    print(0)

